/**************************************************************************
 * Copyright (C) 2011 Tyler Robertson
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/



/*************************************************************
 * Implements basic functions of the wifly device as seen
 * from the perspective of the master microcontroller.
 * 
 * This class takes care of initializing and communicating
 * with the SC16I740 SPI <--> UART bridge automatically.
 *************************************************************/

#ifndef __WIFLYDEVICE_H__
#define __WIFLYDEVICE_H__

#include "Common/packet_types.h"

	
/* All slaves should be initialized before the wifly device otherwise 
 * the SC16I740 bridge will hang and will fail to initialize correctly
 */
void wifly_initialize();

void wifly_send_byte( unsigned char data );
unsigned char wifly_receive_byte();

void wifly_send_data( const unsigned char *data, unsigned int size_in_bytes );
void wifly_receive_data( unsigned char *data, unsigned int size_in_bytes );

// Takes care of creating and sending an ascii packet from a variable argument list
void wifly_send_ascii_packet( const char *fmt, ... );

/* Returns true if there is data availale in the receive buffer */
bool wifly_has_data_available();


/* Gets the number of bytes in the receive FIFO register. Returns 0-64 */
unsigned char wifly_get_num_bytes_available();

/***** Send packets from microcontroller *****/
void wifly_send_packet( const ascii_packet_t *ascii_packet ); 
void wifly_send_packet( const statistics_packet_t *state_packet );
void wifly_send_packet( const timer_op_packet_t *timer_op_packet );
void wifly_send_packet( const command_packet_t *command_packet );
void wifly_send_packet( const state_packet_t *state_packet );

/***** Receive packets from client *****/
//bool wifly_receive_packet( ascii_packet_t *ascii_packet ); 
bool wifly_receive_packet( statistics_packet_t *state_packet );
bool wifly_receive_packet( timer_op_packet_t *timer_op_packet );
bool wifly_receive_packet( command_packet_t *command_packet );
bool wifly_receive_packet( state_packet_t *state_packet );

#endif/*  __WIFLYDEVICE_H__ */
